package dhm.com.dhmshop.view.main;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.MainVpFgAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetGoodsFreightBean;
import dhm.com.dhmshop.fragment.home.HomeFragment;
import dhm.com.dhmshop.fragment.shoppingcart.ShoppingcartFragment;
//import dhm.com.dhmshop.fragment.type.ClassIficationFragment;
//import dhm.com.dhmshop.fragment.wode.WodeFragment;
//import dhm.com.dhmshop.framework.module.home.fragment.HomeFragment;
import dhm.com.dhmshop.framework.module.type.fragment.ClassIficationFragment;
import dhm.com.dhmshop.updata.fragment.WodeFragment;
import dhm.com.dhmshop.utils.SpUtils;

public class MainActivity extends BaseActiity implements View.OnClickListener , LoginContract.IView {

    private ViewPager mMainVp;
    private TabLayout mMainTablayout;
    private LinearLayout mMainContainer;

    private ArrayList<String> titles;
    private ArrayList<Fragment> fragments;
    private HomeFragment homeFragment;
    private ClassIficationFragment classificationFragment;
    private ShoppingcartFragment shoppingcartFragment;
    private WodeFragment wodeFragment;
    private MainVpFgAdapter mainVpFgAdapter;
    private GetGoodsFreightBean bean;

    private PressenterImpl pressenter;
    private TextView message_fu;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @SuppressLint("NewApi")
    @Override
    protected void initView() {

//        getWindow().setNavigationBarColor(Color.BLACK);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        mMainVp = (ViewPager) findViewById(R.id.main_vp);
        mMainTablayout = (TabLayout) findViewById(R.id.main_tablayout);
        mMainContainer = (LinearLayout) findViewById(R.id.main_container);
        mMainVp.setOnClickListener(this);
        mMainTablayout.setOnClickListener(this);

        mMainTablayout.setSelectedTabIndicatorHeight(0);
        //创建tab
        titles = new ArrayList<>();
        titles.add("首页");
        titles.add("分类");
        titles.add("购物车");
        titles.add("我的");

        //四个fragments
        fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        classificationFragment = new ClassIficationFragment();
        shoppingcartFragment = new ShoppingcartFragment();
        wodeFragment = new WodeFragment();
        fragments.add(homeFragment);
        fragments.add(classificationFragment);
        fragments.add(shoppingcartFragment);
        fragments.add(wodeFragment);

        //设置适配器
        mainVpFgAdapter = new MainVpFgAdapter(getSupportFragmentManager(), fragments, titles);

        //绑定适配器
        mMainVp.setAdapter(mainVpFgAdapter);
        mMainVp.setOffscreenPageLimit(0);
        //设置联动
        mMainTablayout.setupWithViewPager(mMainVp);

        setupTabIcons();//设置底部TabLayout的item

        mMainTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();

                switch (position) {
                    case 0:
                        getWindow().setStatusBarColor(Color.WHITE);
//                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
                        //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        }
//                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        // 状态栏（以上几行代码必须，参考setStatusBarColor|setNavigationBarColor方法源码）
//                        window.setNavigationBarColor(Color.BLACK);
//                        getWindow().setStatusBarColor(Color.WHITE);
                        break;
                    case 1:
                        getWindow().setStatusBarColor(Color.WHITE);
//                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
                        //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        }
//                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        // 状态栏（以上几行代码必须，参考setStatusBarColor|setNavigationBarColor方法源码）
//                        window.setNavigationBarColor(Color.BLACK);
//                        getWindow().setStatusBarColor(Color.WHITE);
                        break;
                    case 2:
                        getWindow().setStatusBarColor(Color.WHITE);
//                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
                        //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        }
//                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        // 状态栏（以上几行代码必须，参考setStatusBarColor|setNavigationBarColor方法源码）
//                        window.setNavigationBarColor(Color.BLACK);
//                        getWindow().setStatusBarColor(Color.WHITE);
                        break;
                    case 3:
                        getWindow().setStatusBarColor(getResources().getColor(R.color.main));

                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

//                                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
                        }

                        break;
                    default:
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        String uid = SpUtils.getString(this, "uid");
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(this, Constant.ShoppingCount,map,GetGoodsFreightBean.class);

    }

    @Override
    protected void onResume() {
        super.onResume();
        String uid = SpUtils.getString(this, "uid");
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(this, Constant.ShoppingCount,map,GetGoodsFreightBean.class);
    }

    @Override
    protected void initData() {
        String type = getIntent().getStringExtra("type");
        if (type != null && type.equals("1")) {
            mMainVp.setCurrentItem(2);
        }
    }

    //有几个底部的item就写几个
    private void setupTabIcons() {
        //tablayout图文效果
        for (int i = 0; i < fragments.size(); i++) {
            mMainTablayout.getTabAt(i).setCustomView(getView(i));
        }
    }

    @SuppressLint("NewApi")
    private View getView(int position) {
        View tabitem = LayoutInflater.from(this).inflate(R.layout.tabitem, null);
        ImageView iv = (ImageView) tabitem.findViewById(R.id.tabiv);
        TextView tv = (TextView) tabitem.findViewById(R.id.tabtv);
        TextView message_fu = (TextView) tabitem.findViewById(R.id.message_fu);
        tv.setText(titles.get(position));
        ArrayList<Integer> images = new ArrayList<>();
        images.add(R.drawable.selector_show);
        images.add(R.drawable.selector_classification);
        images.add(R.drawable.selector_shoppingcart);
        images.add(R.drawable.selector_wode);
        iv.setImageResource(images.get(position));
        if (position==2){
                this.message_fu = (TextView) tabitem.findViewById(R.id.message_fu);
                message_fu.setVisibility(View.VISIBLE);
        }else {
            message_fu.setVisibility(View.GONE);
        }
        if (bean==null){
            message_fu.setVisibility(View.GONE);
        }
        return tabitem;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_vp:
                break;
            case R.id.main_tablayout:
                break;
            default:
        }
    }


    /**
     * 防止误触退出
     */
    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();

            } else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetGoodsFreightBean){
            bean = (GetGoodsFreightBean) data;
            if (bean.getData()==0){
                message_fu.setVisibility(View.GONE);
            }else {
                message_fu.setText(bean.getData()+"");
                message_fu.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    public void initdata() {
        String uid = SpUtils.getString(this, "uid");
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(this, Constant.ShoppingCount,map,GetGoodsFreightBean.class);
    }
}
